{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8.0\n",
      "end!\n"
     ]
    }
   ],
   "source": [
    "# placehold\n",
    "import tensorflow as tf\n",
    "data1 = tf.placeholder(tf.float32)\n",
    "data2 = tf.placeholder(tf.float32)\n",
    "dataAdd = tf.add(data1,data2)\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run(dataAdd,feed_dict={data1:6,data2:2}))\n",
    "    # 1 dataAdd  2 data (feed_dict = {arg1: arg1_v arg2: arg2_v})\n",
    "print(\"end!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 2)\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[1 2]\n",
      "[1 3 5]\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "## 矩阵的定义 [[6,6],[6,6]]\n",
    "\n",
    "import tensorflow as tf\n",
    "data1 = tf.constant([[6,6]])\n",
    "data2 = tf.constant([[2],\n",
    "                   [2]])\n",
    "data3 = tf.constant([[3,3]])\n",
    "data4 = tf.constant([[1,2],\n",
    "                   [3,4],\n",
    "                   [5,6]])\n",
    "print(data4.shape) # 维度\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run(data4))\n",
    "    print(sess.run(data4[0]))    # 打印某一行\n",
    "    print(sess.run(data4[:,0]))  # 打印某一列\n",
    "    print(sess.run(data4[0,0]))  # 打印某个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[24]]\n",
      "[[9 9]]\n",
      "[[12 12]\n",
      " [12 12]]\n",
      "[array([[24]]), array([[9, 9]])]\n",
      "[[4 5]\n",
      " [6 7]\n",
      " [8 9]]\n"
     ]
    }
   ],
   "source": [
    "## 矩阵的运算\n",
    "import tensorflow as tf\n",
    "data1 = tf.constant([[6,6]])\n",
    "\n",
    "data2 = tf.constant([[2],\n",
    "                     [2]])\n",
    "\n",
    "data3 = tf.constant([[3,3]])\n",
    "\n",
    "data4 = tf.constant([[1,2],\n",
    "                     [3,4],\n",
    "                     [5,6]])\n",
    "\n",
    "matMul = tf.matmul(data1,data2)\n",
    "datMul2 = tf.multiply(data1,data2)  # 这个也能出结果\n",
    "matAdd = tf.add(data1,data3)\n",
    "da = data4 + data3\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run(matMul))\n",
    "    print(sess.run(matAdd))\n",
    "    print(sess.run(datMul2))\n",
    "    print(sess.run([matMul,matAdd]))\n",
    "    print(sess.run(da))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0]\n",
      " [0 0 0]]\n",
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]]\n",
      "[[1. 1.]\n",
      " [1. 1.]\n",
      " [1. 1.]]\n",
      "[[15 15 15]\n",
      " [15 15 15]]\n"
     ]
    }
   ],
   "source": [
    "## 快速定义矩阵\n",
    "import tensorflow as tf\n",
    "mat0 = tf.constant([[0,0,0],[0,0,0]])\n",
    "mat1 = tf.zeros([2,3])\n",
    "mat2 = tf.ones([3,2])\n",
    "mat3 = tf.fill([2,3],15)   # 定义填充矩阵\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run(mat0))\n",
    "    print(sess.run(mat1))\n",
    "    print(sess.run(mat2))\n",
    "    print(sess.run(mat3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2]\n",
      " [3]\n",
      " [4]]\n",
      "[[0]\n",
      " [0]\n",
      " [0]]\n",
      "[0.        0.2       0.4       0.6       0.8       1.        1.2\n",
      " 1.4       1.6       1.8000001 2.       ]\n",
      "[[ 1.3024216  -0.90472686 -0.52152896]\n",
      " [ 1.1009126   1.9550686  -0.73180985]]\n"
     ]
    }
   ],
   "source": [
    "## 快速定义特殊矩阵\n",
    "import tensorflow as tf\n",
    "mat1 = tf.constant([[2],[3],[4]])\n",
    "mat2 = tf.zeros_like(mat1)\n",
    "mat3 = tf.linspace(0.0,2.0,11)  ## 0 - 2 11等分\n",
    "mat4 = tf.random_uniform([2,3],-1,2)  ## 随机矩阵\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run(mat1))\n",
    "    print(sess.run(mat2))\n",
    "    print(sess.run(mat3))\n",
    "    print(sess.run(mat4))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
